package com.foo.ui_demo_widge;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

public class ActionModeDemoActivity extends AppCompatActivity {
    private ActionMode actionMode;


    private ActionMode.Callback actionModeCallback= new ActionMode.Callback() {
            @Override
            public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
                actionMode.getMenuInflater().inflate(R.menu.action_menu,menu);
                return true;
            }

            @Override
            public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
                return false;
            }

            @Override
            public boolean onActionItemClicked(ActionMode actionMode, MenuItem item) {
                int itemId = item.getItemId();
                if (itemId==R.id.menu_edit||itemId==R.id.menu_mark||itemId==R.id.menu_delete){
                    Toast.makeText(ActionModeDemoActivity.this,item.getTitle(),Toast.LENGTH_SHORT).show();
                    actionMode.finish();
                    return true;
                }
                return false;
            }

            @Override
            public void onDestroyActionMode(ActionMode actionMode) {
                actionMode=null;
            }
        };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_action_mode_demo);
        findViewById(R.id.lv_action_mode_demo).setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                if (actionMode !=null){
                    return false;
                }
                actionMode=ActionModeDemoActivity.this.startActionMode(actionModeCallback);
                view.setSelected(true);
                return true;
            }
        });
    }
}